/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain menu_text_color copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gzwanchuang.xhgj.ui.page.login;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.request.LoginRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.login.PlaceSelectViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentPlaceSelectBinding;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;


/**
 * Create by KunMinX at 19/10/29
 */
public class PlaceSelectFragment extends BaseFragment {

    private FragmentPlaceSelectBinding mBinding;
    private PlaceSelectViewModel mViewModel;
    private LoginRequestViewModel mLoginRequestViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(PlaceSelectViewModel.class);
        mLoginRequestViewModel = ViewModelProviders.of(this).get(LoginRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_place_select, container, false);
        mBinding = FragmentPlaceSelectBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(getContext()));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLoginRequestViewModel.getErrorString().observe(this, this::showShortToast);  // 错误返回
        mLoginRequestViewModel.getAreas().observe(this, datas -> {
            mViewModel.madapter.get().replaceData(datas);
            mViewModel.madapter.get().getLoadMoreModule().loadMoreEnd(true);
        });
        mLoginRequestViewModel.area_list("0");
    }


    public class ClickProxy {
        public void submit() {
        }

        public void back() {
            nav().navigateUp();
        }

        public void itemOnClick(int position) {
            mViewModel.areaList.add(mViewModel.datas.get(position));
            if (mViewModel.selectTime < 2) {
                mViewModel.selectTime++;
                mLoginRequestViewModel.area_list(mViewModel.datas.get(position).getAreaId());
            } else {
                getSharedViewModel().mPlace.setValue(mViewModel.areaList);
                nav().navigateUp();
            }
        }

        public void loadMoreListener() {

        }
    }
}
